package UserProfile;

import java.awt.GridLayout;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class MainFrame extends JFrame{
	
	JPanel panelUp = new JPanel();
	JPanel panelDown = new JPanel();
	JPanel emptyPanel  = new JPanel();;
	JTextField userName = new JTextField("Kullanici Adi : ");
	JTextField password = new JTextField("Sifre         : "); 
	JTextField enterUserName = new JTextField();
	JTextField enterPassword = new JTextField();
	JButton okButton = new JButton("TAMAM");
	JButton newUser = new JButton("Yeni Kullanici");
	JButton forgotPassword = new JButton("Sifremi Unuttum");
	UserData userData = new UserData();
	
	
	public MainFrame() {
		super("GIRIS EKRANI");
		setLocationRelativeTo(null);
		setSize(300, 200);
		setResizable(false);
		
		setLayout(new GridLayout(2, 1));
		
		userName.setEditable(false);
		password.setEditable(false);
		panelUp.setLayout(new GridLayout(2, 2));
		panelUp.add(userName);
		panelUp.add(enterUserName);
		panelUp.add(password);
		panelUp.add(enterPassword);
		
		panelDown.setLayout(new GridLayout(2,2));
		panelDown.add(emptyPanel);
		panelDown.add(okButton);
		panelDown.add(newUser);
		panelDown.add(forgotPassword);
		
		add(panelUp);
		add(panelDown);
		
		okButton.addActionListener(new okButtonListener());
		newUser.addActionListener(new newUserActionListener());
		forgotPassword.addActionListener(new ForgotPasswordListener());
		
	}
	
	public class okButtonListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if(userData.contains(enterUserName.getText()) && 
					userData.checkPassword(enterUserName.getText(), enterPassword.getText())) {
				EntryFrame newEntry = new EntryFrame(enterUserName.getText());
				newEntry.setVisible(true);
				setVisible(false);
			}
			else {
				JOptionPane.showMessageDialog(null, "Hatali kullanici adi ve ya sifre!!!");
			}	
		}	
	}
	
	public class newUserActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {	
			NewUserFrame newUserFrame = new NewUserFrame();
			newUserFrame.setVisible(true);
			setVisible(false);
		}
		
	}
	
	public class ForgotPasswordListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			ForgotPasswordFrame fpf = new ForgotPasswordFrame();
			fpf.setVisible(true);
			setVisible(false);
			
		}
		
	}
}
